$(document).ready(function() {
	 readOnly();
	  
	if( $("#selectedBankCode").val()==""){
		 $("form").attr('action',contextRoot+"/bankdetails/add.html");
		 $("#deActivate").attr('disabled', 'disabled');
	}	
	 $("#selectedBankCode").change(function () {
         var bankCode = $(this).val();
         //alert(bankCode);
         location.href=contextRoot+"/bankdetails/view.html?bankCode="+ bankCode;
       });
	 
	 $("#add").click(function () {
		 $("#selectedBankCode").val("");
		 location.href=contextRoot+"/bankdetails/view.html";
	 });
	 $("#save").click(function () {
		 $("#bankDetails_activeInd").val("Y");		 
		 $("form").attr('method',"GET");
		 $("form").attr('action',contextRoot+"/bankdetails/update.html")
	 });
	 $("#activate").click(function () {
		 enablefields();
		 if(!checkBank() && !checkContact() && !checkAddress() && !checkPhone() && !checkZip() && !checkCountry() && !checkDateFields()){
			 $("#bankDetails_activeInd").val("Y");
			 $("form").attr('action',contextRoot+"/bankdetails/update.html");
		 }
		 else{
			 return false;
		 } 			 
	 });	 
	 $("#deActivate").click(function () {
		 $("#bankDetails_activeInd").val("N");
		 $("form").attr('method',"GET");
		 $("form").attr('action',contextRoot+"/bankdetails/update.html");
	 });
	 $("#updateBtn").click(function () {
		 enablefields();
		 if(!checkBank() && !checkContact() && !checkAddress() && !checkPhone() && !checkZip() && !checkCountry() && !checkDateFields()){
			 $("#bankDetails_activeInd").val("Y");		
			 $("form").attr('action',contextRoot+"/bankdetails/update.html")
		 }
		 else{
			 return false;
		 }			 
	 });
	 
	 $("#addBtn").click(function () {
		 if(!checkBank() && !checkContact() && !checkAddress() && !checkPhone() && !checkZip() && !checkCountry() && !checkDateFields()){
			 $("#bankDetails_activeInd").val("Y");		 
			 $("form").attr('action',contextRoot+"/bankdetails/add.html");
		 }
		 else{
			 return false;
		 } 
	 });
	 $("#cancelBtn").click(function () {
		 $("form").attr('method',"GET");		 
		 $("form").attr('action',contextRoot+"/bankdetails/view.html")
	 });	 
	 
	});
	
	function set(bank){
			$(bank).val($("#selectedBankCode").val());
	}
	function readOnly(){
			if( $("#bankDetails_activeInd").val()=="N"){
				$('#leftDiv :input').attr('disabled', true);
				$('#rightDiv :input').attr('disabled', true);				
			}
	}
	function enablefields(){
		$('#leftDiv :input').removeAttr('disabled');
		$('#rightDiv :input').removeAttr('disabled');				
	}	
	function checkBank() {
		$(".error").hide();
		var hasError = false;
		var bankName = $("#bankName").val();
		var bankCode = $("#bankCode").val();
		if (bankName == "") {
			$("#validation")
					.after(
							'<span class="error" style="color: red">Bank Name is mandatory.</span>');
			hasError = true;
		}else if (bankCode == "" || isNaN(bankCode)) {
			$("#validation")
			.after(
					'<span class="error" style="color: red">Bank Code is mandatory and numeric.</span>');
			hasError = true;
		}
		return hasError;
	}
	function checkContact() {
		$(".error").hide();
		var hasError = false;
		var contact = $("#contact").val();		
		if (contact == "") {
			$("#validation")
					.after(
							'<span class="error" style="color: red">Contact Person is mandatory.</span>');
			hasError = true;
		}
		return hasError;
	}	
	function checkAddress() {
		$(".error").hide();
		var hasError = false;
		var houseNumber = $("#houseNumber").val();	
		if (houseNumber == "") {
			$("#validation")
					.after(
							'<span class="error" style="color: red">House Number is mandatory.</span>');
			hasError = true;
		}
		return hasError;
	}	
	function checkPhone() {
		$(".error").hide();
		var hasError = false;
		var phone = $("#phone").val();
		var email = $("#email").val();		
		if (phone == "" && email == "") {
			$("#validation")
					.after(
							'<span class="error" style="color: red">Either Phone Number or Email Address is mandatory.</span>');
			hasError = true;
		}else if (email != "" && validateEmail(email)) {
			$("#validation")
			.after(
					'<span class="error" style="color: red">Email address is mandatory or not valid.</span>');
			hasError = true;
		}
		return hasError;
	}	
	function checkZip() {
		$(".error").hide();
		var hasError = false;
		var zipCode = $("#zipCode").val();
		var city = $("#city").val();		
		if (zipCode == "") {
			$("#validation")
					.after(
							'<span class="error" style="color: red">Zipcode is mandatory and numeric.</span>');
			hasError = true;
		}else if (city == "") {
			$("#validation")
			.after(
					'<span class="error" style="color: red">City is mandatory or not valid.</span>');
			hasError = true;
		}
		return hasError;
	}	
	function checkCountry() {
		$(".error").hide();
		var hasError = false;
		var country = $("#country").val();		
		if (country == "") {
			$("#validation")
					.after(
							'<span class="error" style="color: red">Country is mandatory.</span>');
			hasError = true;
		}
		return hasError;
	}	
	function validateEmail(email) {
	    var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	    if( !emailReg.test( email ) ) {
	        return true;
	    } else {
	        return false;
	    }
	}
	function checkDateFields(){
		$(".error").hide();
		var hasError = false;		
		var fromDate = $("#fromdt").val();	
		var toDate = $("#todt").val();	
		if (fromDate == "" || toDate == "") {
			$("#validation")
					.after(
							'<span class="error" style="color: red">Date fields are mandatory</span>');
			hasError = true;
		}else if (Date.parse(fromDate) >= Date.parse(toDate)) {
			$("#validation")
			.after(
					'<span class="error" style="color: red">Start Date must be lesser than End Date</span>');
			hasError = true;
		}
		return hasError;
	}